home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / CONSOLE.C < prev    next >
Text File  |  1991-09-26  |  6KB  |  291 lines

  1. /* ----------- console.c ---------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. /* ----- table of alt keys for finding shortcut keys ----- */
  6. static int altconvert[] = {
  7.     ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
  8.     ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
  9.     ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
  10.     ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
  11.     ALT_6,ALT_7,ALT_8,ALT_9
  12. };
  13.  
  14. unsigned video_mode;
  15. unsigned video_page;
  16.  
  17. static int near cursorpos[MAXSAVES];
  18. static int near cursorshape[MAXSAVES];
  19. static int cs;
  20.  
  21. static union REGS regs;
  22.  
  23. void initvideo(void)
  24. {
  25.     if (isEGA() || isVGA())    {
  26.         regs.x.ax = 0x1003;
  27.         regs.h.bl = 0;
  28.         int86(VIDEO, ®s, ®s);
  29.     }
  30. }
  31.  
  32. void restorevideo(void)
  33. {
  34.     if (isEGA() || isVGA())    {
  35.         regs.x.ax = 0x1003;
  36.         regs.h.bl = 1;
  37.         int86(VIDEO, ®s, ®s);
  38.     }
  39. }
  40.  
  41. void SwapCursorStack(void)
  42. {
  43.     if (cs > 1)    {
  44.         swap(cursorpos[cs-2], cursorpos[cs-1]);
  45.         swap(cursorshape[cs-2], cursorshape[cs-1]);
  46.     }
  47. }
  48.  
  49. #ifndef MSC
  50. #ifndef WATCOM
  51. #define ZEROFLAG 0x40
  52. /* ---- Test for keystroke ---- */
  53. int keyhit(void)
  54. {
  55.     _AH = 1;
  56.     geninterrupt(KEYBRD);
  57.     return (_FLAGS & ZEROFLAG) == 0;
  58. }
  59. #endif
  60. #endif
  61.  
  62. /* ---- Read a keystroke ---- */
  63. int getkey(void)
  64. {
  65.     int c;
  66.     while (keyhit() == 0)
  67.         ;
  68.     if (((c = bioskey(0)) & 0xff) == 0)
  69.         c = (c >> 8) | 0x1080;
  70.     else
  71.         c &= 0xff;
  72.     return c & 0x10ff;
  73. }
  74.  
  75. /* ---------- read the keyboard shift status --------- */
  76. int getshift(void)
  77. {
  78.     regs.h.ah = 2;
  79.     int86(KEYBRD, ®s, ®s);
  80.     return regs.h.al;
  81. }
  82.  
  83. static int far *clk = MK_FP(0x40,0x6c);
  84. /* ------- macro to wait one clock tick -------- */
  85. #define wait()          \
  86. {                       \
  87.     int now = *clk;     \
  88.     while (now == *clk) \
  89.         ;               \
  90. }
  91.  
  92. /* -------- sound a buzz tone ---------- */
  93. void beep(void)
  94. {
  95.     wait();
  96.     outp(0x43, 0xb6);               /* program the frequency */
  97.     outp(0x42, (int) (COUNT % 256));
  98.     outp(0x42, (int) (COUNT / 256));
  99.     outp(0x61, inp(0x61) | 3);      /* start the sound */
  100.     wait();
  101.     outp(0x61, inp(0x61) & ~3);     /* stop the sound  */
  102. }
  103.  
  104. /* -------- get the video mode and page from BIOS -------- */
  105. void videomode(void)
  106. {
  107.     regs.h.ah = 15;
  108.     int86(VIDEO, ®s, ®s);
  109.     video_mode = regs.h.al;
  110.     video_page = regs.x.bx;
  111.     video_page &= 0xff00;
  112.     video_mode &= 0x7f;
  113. }
  114.  
  115. /* ------ position the cursor ------ */
  116. void cursor(int x, int y)
  117. {
  118.     videomode();
  119.     regs.x.dx = ((y << 8) & 0xff00) + x;
  120.     regs.h.ah = SETCURSOR;
  121.     regs.x.bx = video_page;
  122.     int86(VIDEO, ®s, ®s);
  123. }
  124.  
  125. /* ------ get cursor shape and position ------ */
  126. static void near getcursor(void)
  127. {
  128.     videomode();
  129.     regs.h.ah = READCURSOR;
  130.     regs.x.bx = video_page;
  131.     int86(VIDEO, ®s, ®s);
  132. }
  133.  
  134. /* ------- get the current cursor position ------- */
  135. void curr_cursor(int *x, int *y)
  136. {
  137.     getcursor();
  138.     *x = regs.h.dl;
  139.     *y = regs.h.dh;
  140. }
  141.  
  142. /* ------ save the current cursor configuration ------ */
  143. void savecursor(void)
  144. {
  145.     if (cs < MAXSAVES)    {
  146.         getcursor();
  147.         cursorshape[cs] = regs.x.cx;
  148.         cursorpos[cs] = regs.x.dx;
  149.         cs++;
  150.     }
  151. }
  152.  
  153. /* ---- restore the saved cursor configuration ---- */
  154. void restorecursor(void)
  155. {
  156.     if (cs)    {
  157.         --cs;
  158.         videomode();
  159.         regs.x.dx = cursorpos[cs];
  160.         regs.h.ah = SETCURSOR;
  161.         regs.x.bx = video_page;
  162.         int86(VIDEO, ®s, ®s);
  163.         set_cursor_type(cursorshape[cs]);
  164.     }
  165. }
  166.  
  167. /* ------ make a normal cursor ------ */
  168. void normalcursor(void)
  169. {
  170.     set_cursor_type(0x0607);
  171. }
  172.  
  173. /* ------ hide the cursor ------ */
  174. void hidecursor(void)
  175. {
  176.     getcursor();
  177.     regs.h.ch |= HIDECURSOR;
  178.     regs.h.ah = SETCURSORTYPE;
  179.     int86(VIDEO, ®s, ®s);
  180. }
  181.  
  182. /* ------ unhide the cursor ------ */
  183. void unhidecursor(void)
  184. {
  185.     getcursor();
  186.     regs.h.ch &= ~HIDECURSOR;
  187.     regs.h.ah = SETCURSORTYPE;
  188.     int86(VIDEO, ®s, ®s);
  189. }
  190.  
  191. /* ---- use BIOS to set the cursor type ---- */
  192. void set_cursor_type(unsigned t)
  193. {
  194.     videomode();
  195.     regs.h.ah = SETCURSORTYPE;
  196.     regs.x.bx = video_page;
  197.     regs.x.cx = t;
  198.     int86(VIDEO, ®s, ®s);
  199. }
  200.  
  201. /* ---- test for EGA -------- */
  202. int isEGA(void)
  203. {
  204.     if (isVGA())
  205.         return 0;
  206.     regs.h.ah = 0x12;
  207.     regs.h.bl = 0x10;
  208.     int86(VIDEO, ®s, ®s);
  209.     return regs.h.bl != 0x10;
  210. }
  211.  
  212. /* ---- test for VGA -------- */
  213. int isVGA(void)
  214. {
  215.     regs.x.ax = 0x1a00;
  216.     int86(VIDEO, ®s, ®s);
  217.     return regs.h.al == 0x1a && regs.h.bl > 6;
  218. }
  219.  
  220. static void Scan350(void)
  221. {
  222.     regs.x.ax = 0x1201;
  223.     regs.h.bl = 0x30;
  224.     int86(VIDEO, ®s, ®s);
  225. }
  226.  
  227. static void Scan400(void)
  228. {
  229.     regs.x.ax = 0x1202;
  230.     regs.h.bl = 0x30;
  231.     int86(VIDEO, ®s, ®s);
  232. }
  233.  
  234. /* ---------- set 25 line mode ------- */
  235. void Set25(void)
  236. {
  237.     if (isVGA())    {
  238.         Scan400();
  239.         regs.x.ax = 0x1114;
  240.     }
  241.     else
  242.         regs.x.ax = 0x1111;
  243.     regs.x.bx = 0;
  244.     int86(VIDEO, ®s, ®s);
  245. }
  246.  
  247. /* ---------- set 43 line mode ------- */
  248. void Set43(void)
  249. {
  250.     if (isVGA())
  251.         Scan350();
  252.     regs.x.ax = 0x1112;
  253.     regs.x.bx = 0;
  254.     int86(VIDEO, ®s, ®s);
  255. }
  256.  
  257. /* ---------- set 50 line mode ------- */
  258. void Set50(void)
  259. {
  260.     if (isVGA())
  261.         Scan400();
  262.     regs.x.ax = 0x1112;
  263.     regs.x.bx = 0;
  264.     int86(VIDEO, ®s, ®s);
  265. }
  266.  
  267. /* ------ convert an Alt+ key to its letter equivalent ----- */
  268. int AltConvert(int c)
  269. {
  270.     int i, a = 0;
  271.     for (i = 0; i < 36; i++)
  272.         if (c == altconvert[i])
  273.             break;
  274.     if (i < 26)
  275.         a = 'a' + i;
  276.     else if (i < 36)
  277.         a = '0' + i - 26;
  278.     return a;
  279. }
  280.  
  281. #if MSC | WATCOM
  282. int getdisk(void)
  283. {
  284.     unsigned int cd;
  285.     _dos_getdrive(&cd);
  286.     cd -= 1;
  287.     return cd;
  288. }
  289. #endif
  290.  
  291.